<template>
    <div class='edit'>
        <p-navigator 
            text="修改地址"
            is-btn
            btn-text="删除"
            :btn-style="{
                color:'red'
            }"
            @click="del()"
            />
            <van-address-edit
                :address-info="desc"
                :area-list="areaList"
                show-set-default
                @save="submit"
            />
    </div>
</template>
<script>
// 引入vant的省市区数据
import { areaList } from '@vant/area-data';
import {getAddressDesc,deleteAddressDesc,editAddress} from '@/api'
export default {
    methods: {
        submit(item){
            item.a_provice = item.province;
            item.a_city = item.city;
            item.a_code = item.county;
            item.phone = item.tel;
            item.a_desc = item.addressDetail;
            item.is_select = Number(item.isDefault);
            item.address_id = item.ID;
            // 调用修改地址信息的接口
            editAddress(item).then(res => {
                if(res.data.code == 1){
                    this.$toast('修改成功');
                    setTimeout(() => {
                        this.$router.back()
                    }, 1000);
                }
            })
        },
        getDesc(address_id){
            getAddressDesc({address_id}).then(res => {
                if(res.data.code == 1){
                    res.data.result[0].tel = res.data.result[0].phone;
                    res.data.result[0].addressDetail = res.data.result[0].a_desc;
                    res.data.result[0].isDefault = Boolean(res.data.result[0].is_select);
                    this.desc = res.data.result[0];
                }
            })
        },
        del(){
            // vant弹出框
            this.$dialog.confirm({
                // 确认框的标题
                title: '提示',
                // 确认框的内容
                message: '删除后，操作不可逆，确认删除吗？',
            }).then(() => {
                deleteAddressDesc( {
                    address_id: this.$route.params.address_id
                }).then(res => {
                    if(res.data.code == 1){
                        // 
                        this.$toast('删除成功');
                        // 延迟返回
                        setTimeout(() => {
                            this.$router.back()
                        }, 1000);
                    }
                })
            }).catch(() => {
                // 点击取消按钮回调
                this.$toast('取消退出')
            })
        }
    },
    created(){
        // 路由信息
        this.getDesc(this.$route.params.address_id)
    },
    data() {
        return {
            areaList,
            desc: {
                name: '',
                tel: ''
            }
        }
    },
}
</script>
<style lang="">
    
</style>